import os,subprocess,time
import pyautogui
import psutil
from PIL import Image
import pyscreeze,cv2
import pinyin
import os,subprocess



def getButton(targetImage):
    # 屏幕缩放系数 mac缩放是2 windows一般是1
    screenScale = 1
    # 事先读取按钮截图
    target = cv2.imread(targetImage, cv2.IMREAD_GRAYSCALE)
    # 先截图
    screenshot = pyscreeze.screenshot('my_screenshot.png')
    # 读取图片 灰色会快
    temp = cv2.imread(r'my_screenshot.png', cv2.IMREAD_GRAYSCALE)
    theight, twidth = target.shape[:2]
    tempheight, tempwidth = temp.shape[:2]
    # 先缩放屏幕截图 INTER_LINEAR INTER_AREA
    scaleTemp = cv2.resize(temp, (int(tempwidth / screenScale), int(tempheight / screenScale)))
    stempheight, stempwidth = scaleTemp.shape[:2]
    # 匹配图片
    res = cv2.matchTemplate(scaleTemp, target, cv2.TM_CCOEFF_NORMED)
    mn_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    print(max_val)
    if (max_val >= 0.9):
        # 计算出中心点
        top_left = max_loc
        bottom_right = (top_left[0] + twidth, top_left[1] + theight)
        tagHalfW = int(twidth / 2)
        tagHalfH = int(theight / 2)
        tagCenterX = top_left[0] + tagHalfW
        tagCenterY = top_left[1] + tagHalfH
        # 左键点击屏幕上的这个位置
        pyautogui.click(tagCenterX, tagCenterY, button='left')
    else:
        print("没找到")





def creatOQ(clientName,OQAccount,OQAccountType):
    accountName=pinyin.get(clientName)
    OQTradeAccount=OQAccount
    actionList=[]
    if OQAccount =='':
        if OQAccountType =='金证极速股票':
            OQTradeAccount=random.choice(['10000068687','10068130123'])
            actionList=['OQ_none_1.jpg','Tab','Tab','accountName','clientName','Tab','Tab','Tab','Tab','Tab','SZ001','OQ_none_2.jpg','空格','enter','Tab','Tab','enter','enter','esc',
                        'OQ_none_3.jpg','OQ_none_4.jpg','OQTradeAccount','enter','down40','Tab','Tab','Tab','enter','enter','OneQuant','Tab','Tab','accountName','Tab','Ax888888','enter',
            'Tab','Ax888888','Tab','1234@Abc','Tab','1234@Abc','enter']
            print(f'OneQuant 账户：{accountName}  密码：1234@Abc   在【我的】-【资产全景】，使用交易密码123321登录资金账号')
        elif OQAccountType =='金证极速信用':
            OQTradeAccount=random.choice(['10000068687','10068130123'])
            actionList=['OQ_none_1.jpg','Tab','Tab','accountName','clientName','Tab','Tab','Tab','Tab','Tab','SZ001','OQ_none_2.jpg','空格','enter','Tab','Tab','enter','enter','esc',
                        'OQ_none_3.jpg','OQ_none_4.jpg','OQTradeAccount','enter','down100','Tab','Tab','Tab','enter','enter','OneQuant','Tab','Tab','accountName','Tab','Ax888888','enter',
            'Tab','Ax888888','Tab','1234@Abc','Tab','1234@Abc','enter']
            print(f'OneQuant 测试账户：{accountName}  密码：1234@Abc   在【我的】-【资产全景】，使用交易密码123321登录资金账号')
    else:
            OQTradeAccount=random.choice(['10000068687','10068130123'])
            actionList=['OQ_none_1.jpg','Tab','Tab','accountName','clientName','Tab','Tab','Tab','Tab','Tab','SZ001','OQ_none_2.jpg','空格','enter','Tab','Tab','enter','enter','esc',
                        'OQ_none_3.jpg','OQ_none_7.jpg','up2','down1','Tab','enter','down100','OQ_none_7.jpg' ,'OQ_none_8.jpg','down4','enter','Tab','OQAccount', 'Tab','OQAccount','Tab',
                        '华锐测试账号','OQ_none_10.jpg','down4','enter','Tab','Tab','enter','enter','esc','OQ_none_3.jpg','OQ_none_4.jpg','OQTradeAccount','enter','down100','Tab','Tab','Tab','enter','enter','OneQuant','Tab','Tab','accountName','Tab','Ax888888','enter',
            'Tab','Ax888888','Tab','1234@Abc','Tab','1234@Abc','enter']
            print(f'OneQuant 测试账户：{accountName}  密码：1234@Abc   在【我的】-【资产全景】，使用交易密码123321登录华锐极速股票账号{OQAccount}')
    for actionRPA in actionList:
        if actionRPA=='enter':
            pyautogui.press('enter')
            time.sleep(2)
        elif actionRPA=='Tab':
            pyautogui.press('Tab')
            time.sleep(2)
        elif actionRPA=='空格':
            pyautogui.press('backspace')
            time.sleep(2)
        elif '.jpg' in actionRPA:
            imageDric=os.path.join(base_dir,'123.txt')    #拼接图片路径
            getButton(actionRPA)
            time.sleep(2)
        elif actionRPA=='esc':
            pyautogui.press('esc')
            time.sleep(2)
        elif 'down' in actionRPA:
            actionNum=actionRPA.rstrip('down')
            for i in range(0,actionNum):
                pyautogui.press('down')
                time.sleep(0.1)
        elif 'up' in actionRPA:
            actionNum=actionRPA.rstrip('up')
            for i in range(0,actionNum):
                pyautogui.press('up')
                time.sleep(0.1)
        elif actionRPA in ['SZ001','华锐测试账号','Ax888888','1234@Abc']:
            pyautogui.typewrite(actionRPA)
            time.sleep(2)            
        elif actionRPA=='accountName':
            pyautogui.typewrite(accountName)
            time.sleep(2)
        elif actionRPA=='clientName':
            pyautogui.typewrite(clientName)
            time.sleep(2)
        elif actionRPA in ['OQTradeAccount','OQAccount']:
            pyautogui.typewrite(OQAccount)
            time.sleep(2)
        elif actionRPA=='OneQuant':      #判断OQ进程是否存在，不存在新开，存在杀掉
            exeNo = os.popen('tasklist /FI "IMAGENAME eq %s"' % 'InfiniTrader.exe').read().count('InfiniTrader.exe')   #判断存在的进程数量
            if exeNo==0:
                pyautogui.typewrite(accountName)
                time.sleep(5)
            else:
                for process in psutil.process_iter():
                    if process.name() == "LeagueClient.exe":
                        process.kill()
print(f'开始自动开户，时间{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}')
creatOQ('张三','','华锐')
print(f'结束自动开户，时间{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}')
